/*
 * ItemMagicRefineResource.cpp
 *
 *  Created on: 2014. 3. 24.
 *      Author: mega
 */

#include "../User.h"
#include "../NLogic/Item.h"
#include "Static.h"
#include "ItemMagicRefineResource.h"
#include <Database/Static.h>
#include <Toolkit/Str.h>
#include <algorithm>

namespace ProjectZ { namespace NResource {

ItemMagicRefineResource::ItemMagicRefineResource()
{
}

ItemMagicRefineResource::~ItemMagicRefineResource()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), [&](MAGICREFINETREE::value_type & pair) {
		delete pair.second;
	});
}

void
ItemMagicRefineResource::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), [&](MAGICREFINETREE::value_type & pair) {
		delete pair.second;
	});
	clsFlyTree.clear();
}

bool
ItemMagicRefineResource::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_item_magic_refine");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		Toolkit::Str<128> DESC;
		ItemMagicRefineResource::Flyweight * pFly = new ItemMagicRefineResource::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_IDX >> pFly->_VALUE >> DESC;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_IDX] = pFly;
	}

	return true;
}

ItemMagicRefineResource::Flyweight *
ItemMagicRefineResource::GetFly(MagicRefineType eType)
{
	int nType = static_cast<int>(eType);

	auto iter = clsFlyTree.find(nType);
	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

bool
ItemMagicRefineResource::GetDefaultRate(NLogic::Item * pItem, int & default_rate)
{
	ItemMagicRefineResource::MagicRefineType eType;

	switch (pItem->GetData().ability_enhance_rate)
	{
	case 0 : eType = ItemMagicRefineResource::MagicRefineType::MAGIC_REFINE_RATE_1; break;
	case 1 : eType = ItemMagicRefineResource::MagicRefineType::MAGIC_REFINE_RATE_2; break;
	case 2 : eType = ItemMagicRefineResource::MagicRefineType::MAGIC_REFINE_RATE_3; break;
	case 3 : eType = ItemMagicRefineResource::MagicRefineType::MAGIC_REFINE_RATE_4; break;
	case 4 : eType = ItemMagicRefineResource::MagicRefineType::MAGIC_REFINE_RATE_5; break;
	}

	Flyweight * pFly = GetFly(eType);
	if (NULL == pFly)
	{
		return false;
	}

	default_rate = pFly->_VALUE;

	return true;
}

bool
ItemMagicRefineResource::GetRatePerMagicStone(int & magic_stone_rate)
{
	Flyweight * pFly = GetFly(MagicRefineType::MAGIC_STONE_RATE);
	if (NULL == pFly)
	{
		return false;
	}

	magic_stone_rate = pFly->_VALUE;

	return true;
}

bool
ItemMagicRefineResource::GetItemDefaultUpgradeRate(int & upgrade_rate)
{
	Flyweight * pFly = GetFly(MagicRefineType::DEFAULT_UPGRADE_VALUE);
	if (NULL == pFly)
	{
		return false;
	}

	upgrade_rate = pFly->_VALUE;

	return true;
}

bool
ItemMagicRefineResource::GetItemDestroyRate(User * pUser, int & destory_rate)
{
	if (true == Static::Instance().GetEventTime()->IsMagicRefineDestroyEvent(pUser))
	{
		Flyweight * pFly = GetFly(MagicRefineType::ITEM_DESTROY_RATE);
		if (NULL == pFly)
		{
			return false;
		}

		destory_rate = pFly->_VALUE;
	}
	else
	{
		destory_rate = 100;
	}

	return true;
}

} /* namespace NResource */ } /* namespace ProjectZ */
